import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ijkplayer/flutter_ijkplayer.dart';
import 'package:loxen/api/http_server/dictionaries.dart';
import 'package:loxen/api/http_server/report.dart';
import 'package:loxen/api/http_server/statistics.dart';
import 'package:loxen/dialog/board_dialog.dart';
import 'package:loxen/dialog/toast.dart';
import 'package:loxen/mode/comment_mode.dart';
import 'package:loxen/mode/study/study_info_bean.dart';
import 'package:loxen/router_observer.dart';
import 'package:loxen/routes/routes.dart';
import 'package:loxen/utils/config.dart';
import 'package:loxen/utils/format_time.dart';
import 'package:loxen/utils/image_url.dart';
import 'package:loxen/utils/string_res.dart';
import 'package:loxen/view/inspiration/comment_input_widget.dart';
import 'package:loxen/viewmode/base_view_model.dart';
import 'package:loxen/viewmode/login_view_model.dart';
import 'package:loxen/viewmode/study_detail_viewmodel.dart';
import 'package:loxen/viewmode/study_index_viewmodel.dart';
import 'package:loxen/widget/fail_widget.dart';
import 'package:loxen/widget/loading_widget.dart';
import 'package:loxen/widget/my_subassembly.dart';
import 'package:loxen/widget/photo_view_simple_screen.dart';
import 'package:loxen/widget/triangle_painter.dart';
import 'package:provider/provider.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

enum CommentType {
  COMMENT, //评论
  COMMENT_TO_USER, //回复
}

class CommentValueData {
  bool _commentIsHide;
  String _hintText;
  CommentType _type;
  Comment _comment;
  Comment params;

  CommentValueData(this._commentIsHide, this._hintText, this._type, this._comment, {this.params});

  Comment get comment => _comment;

  CommentType get type => _type;

  String get hintText => _hintText;

  bool get commentIsHide => _commentIsHide;
}

class StudyDetailWidget extends StatefulWidget with StatisticsMixin {
  final StudyInfo detail;
  final int currentCategory;

  StudyDetailWidget({Key key, this.detail, this.currentCategory}) : super(key: key) {
    if (currentCategory == 0) {
      statisticeStudyKnowledge(detail.id.toString());
    } else {
      statisticeStudyCase(detail.id.toString());
    }
  }

  _StudyDetailWidgetState createState() => _StudyDetailWidgetState();
}

// ignore: must_be_immutable
class _StudyDetailWidgetState extends State<StudyDetailWidget> with RouteAware {
  static const Widget empty = const SizedBox.shrink();
  final blue = const Color(0xFF0CABCB);
  final grey = const Color(0xFF999999);

  final CommentValueData hide = CommentValueData(true, '', null, null);

  final ValueNotifier<CommentValueData> commentIsHide =
      ValueNotifier(CommentValueData(true, '', null, null));
  final FocusNode _commentFocus = FocusNode();

  final ScrollController scrollController = ScrollController();
  StudyDetailViewModel model;
  bool isLogin;
  String videoUrl;
  String hintText;
  FocusScopeNode _focusScope;

  IjkMediaController controller = IjkMediaController();

  void moveCommentTop(BuildContext context) {
    int comm = 0;
    context.read<StudyDetailViewModel>().comments.forEach((element) {
      comm += 130;
      element.sub_comment?.forEach((element) {
        comm += 130;
      });
    });
    var like = context.read<StudyDetailViewModel>().likeStudy?.length ?? 0 * 400;
    var jumpTo = scrollController.position.maxScrollExtent - like - comm;
    if (jumpTo > 0) {
      scrollController.jumpTo(jumpTo);
    }
  }

  @override
  void initState() {
    model = StudyDetailViewModel(widget.detail, widget.currentCategory);
    model.loadContent(onSuccess: () {
      controller.setNetworkDataSource(model.videoUrl, autoPlay: true);
    });
    model.loadComment();
    scrollController.addListener(() {
      if (commentIsHide.value.commentIsHide == false) {
        commentIsHide.value = hide;
        _focusScope.unfocus();
      }
      model.changeColor(scrollController.offset);
    });
    super.initState();
  }

  @override
  void didChangeDependencies() {
    routeObserver.subscribe(this, ModalRoute.of(context)); //订阅
    super.didChangeDependencies();
  }

  @override
  void didPopNext() {
    controller.play();
    super.didPopNext();
  }

  @override
  void didPushNext() {
    controller.pause();
    super.didPushNext();
  }

  @override
  void dispose() {
    controller.dispose();
    routeObserver.unsubscribe(this);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    isLogin = Provider.of<LoginViewModel>(context, listen: false).isLogin();
    _focusScope = FocusScope.of(context);
    return ChangeNotifierProvider<StudyDetailViewModel>.value(
      value: model,
      builder: (context, _) {
        return Scaffold(
          extendBodyBehindAppBar: true,
          appBar: PreferredSize(
            preferredSize: Size(double.infinity, 95.h),
            child: Selector<StudyDetailViewModel, int>(
              builder: (context, opacity, __) {
                return Container(
                  color: Theme.of(context).primaryColor.withAlpha(opacity),
                  child: SafeArea(
                    child: Row(
                      children: <Widget>[
                        MySubassembly.getBackIcon(context),
                        Expanded(
                          child: Selector<StudyDetailViewModel, BaseState>(
                              builder: (context, state, __) {
                                if (state == BaseState.CONTENT) {
                                  return Center(
                                    child: Text(
                                      Provider.of<StudyDetailViewModel>(context).contentBean.title,
                                      maxLines: 1,
                                      style: TextStyle(
                                          fontSize: 31.sp, color: Colors.white.withAlpha(opacity)),
                                    ),
                                  );
                                }
                                return Text('');
                              },
                              selector: (_, model) => model.mState),
                        ),
                        MySubassembly.getSearchIcon(null,
                            text: Constants.empty, width: 44.w, height: 8.h)
                      ],
                    ),
                  ),
                );
              },
              selector: (_, model) => model.opacity,
            ),
          ),
          body: Selector<StudyDetailViewModel, BaseState>(
            builder: (context, state, __) {
              if (state == BaseState.CONTENT) {
                return MediaQuery.removePadding(
                    context: context,
                    removeTop: true,
                    child: Container(
                      color: Theme.of(context).backgroundColor,
                      child: Stack(
                        children: <Widget>[
                          ListView.builder(
                              controller: scrollController,
                              itemCount: 1,
                              physics: ClampingScrollPhysics(),
                              itemBuilder: (context, index) => Column(
                                    children: <Widget>[
                                      _getContentWidget(context, 0),
                                      _getContentWidget(context, 1),
                                      _getContentWidget(context, 2),
                                      _getContentWidget(context, 3),
                                      _getContentWidget(context, 4),
                                      _getContentWidget(context, 5),
                                      model.currentCategory != 0
                                          ? _getContentWidget(context, 6)
                                          : empty,
                                      _getContentWidget(context, 7),
                                    ],
                                  )),
                          Align(
                            alignment: Alignment.bottomCenter,
                            child: Padding(
                              padding: EdgeInsets.only(bottom: 50.h),
                              child: Row(
                                mainAxisSize: MainAxisSize.max,
                                children: <Widget>[
                                  Padding(
                                    padding: EdgeInsets.only(left: 36.w),
                                    child: model.currentCategory == 0
                                        ? _customMainButton(
                                            iconPath: 'images/comment_icon.png',
                                            onPressed: () {
                                              if (isLogin) {
                                                Navigator.of(context).pushNamed(Routes.login_page);
                                              } else {
                                                moveCommentTop(context);
                                                commentIsHide.value = CommentValueData(
                                                    false, '评论：', CommentType.COMMENT, null);
                                                _focusScope.requestFocus(_commentFocus);
                                              }
                                            })
                                        : empty,
                                  ),
                                  Expanded(child: Constants.empty),
                                  Padding(
                                    padding: EdgeInsets.only(right: 40.w),
                                    child: Selector<StudyDetailViewModel, int>(
                                        builder: (context, data, __) {
                                          return _customMainButton(
                                              iconPath: 'images/collation_false.png',
                                              selectedIconPath: 'images/collation_true.png',
                                              isSelected: data == 1,
                                              onPressed: () async {
                                                if (isLogin) {
                                                  Navigator.of(context)
                                                      .pushNamed(Routes.login_page);
                                                  return;
                                                }
                                                int type = context
                                                    .read<StudyDetailViewModel>()
                                                    .currentCategory;
                                                if (data == 0) {
                                                  FileType fileType;
                                                  if (type == 0) {
                                                    fileType = FileType.KNOWLEDGE;
                                                  } else {
                                                    fileType = FileType.APPLY;
                                                  }
                                                  Navigator.of(context).pushNamed(
                                                      Routes.master_file_add,
                                                      arguments: {
                                                        'type': fileType,
                                                        'id': widget.detail.id.toString()
                                                      }).then((value) {
                                                    if (value) {
                                                      context
                                                          .read<StudyDetailViewModel>()
                                                          .collection();
                                                    }
                                                  });
                                                } else {
                                                  bool data = await showDialog(
                                                      context: context,
                                                      builder: (context) => AlertDialog(
                                                            title: Text('提示'),
                                                            content: Text(
                                                              '是否取消收藏？',
                                                              style: TextStyle(color: Colors.black),
                                                            ),
                                                            actions: <Widget>[
                                                              FlatButton(
                                                                child:
                                                                    Text(StringResources.qu_xiao),
                                                                onPressed: () =>
                                                                    Navigator.of(context).pop(),
                                                              ),
                                                              FlatButton(
                                                                child:
                                                                    Text(StringResources.que_ding),
                                                                onPressed: () {
                                                                  Navigator.of(context).pop(true);
                                                                },
                                                              ),
                                                            ],
                                                          ));
                                                  if (data != null && data) {
                                                    context
                                                        .read<StudyDetailViewModel>()
                                                        .unCollection(
                                                            (msg) => Toast.show(msg, context));
                                                  }
                                                }
                                              });
                                        },
                                        selector: (_, model) => model.contentBean.is_collect),
                                  ),
                                  Padding(
                                    padding: EdgeInsets.only(right: 36.w),
                                    child: Selector<StudyDetailViewModel, int>(
                                        builder: (context, data, __) {
                                          return _customMainButton(
                                              iconPath: 'images/like_false.png',
                                              selectedIconPath: 'images/like_true.png',
                                              isSelected: data == 1,
                                              onPressed: () {
                                                if (isLogin) {
                                                  Navigator.of(context)
                                                      .pushNamed(Routes.login_page);
                                                } else {
                                                  if (data == 0) {
                                                    context.read<StudyDetailViewModel>().likeThis();
                                                  } else {
                                                    context
                                                        .read<StudyDetailViewModel>()
                                                        .unLikeThis();
                                                  }
                                                }
                                              });
                                        },
                                        selector: (_, model) => model.contentBean.is_like),
                                  )
                                ],
                              ),
                            ),
                          ),
                          ValueListenableProvider.value(
                            value: commentIsHide,
                            builder: (context, _) {
                              CommentValueData data = context.watch<CommentValueData>();
                              return Offstage(
                                offstage: data.commentIsHide,
                                child: Align(
                                  alignment: Alignment.bottomCenter,
                                  child: CommentInputWidget(
                                      onTapComment: (text) {
                                        if (data.type == CommentType.COMMENT) {
                                          context.read<StudyDetailViewModel>().commentThis(text);
                                        } else {
                                          context.read<StudyDetailViewModel>().commentToComment(
                                              data.comment, "${data.hintText}:$text",
                                              params: data.params,
                                              onError: (msg) => Toast.show(msg, context));
                                        }
                                        commentIsHide.value = hide;
                                        _focusScope.unfocus();
                                      },
                                      commentFocus: _commentFocus,
                                      hintText: data.hintText),
                                ),
                              );
                            },
                          )
                        ],
                      ),
                    ));
              }
              if (state == BaseState.LOADING) {
                return LoadingWidget();
              }
              if (state == BaseState.FAIL) {
                return FailWidget(onClick: () {});
              }
              return Text('');
            },
            selector: (_, model) => model.mState,
          ),
        );
      },
    );
  }

  Widget _customMainButton({iconPath, selectedIconPath, isSelected = false, onPressed}) {
    return GestureDetector(
      onTap: onPressed,
      child: Container(
        alignment: Alignment.center,
        width: 100.w,
        height: 100.w,
        child: Image.asset(
          !isSelected ? iconPath : selectedIconPath,
          width: 100.w,
          height: 100.w,
        ),
      ),
    );
  }

  //主体内容
  Widget _getContentWidget(BuildContext context, int index) {
    StudyDetailViewModel model = Provider.of<StudyDetailViewModel>(context, listen: false);
//    fplayer.setDataSource(videoUrl != null ? videoUrl : "", autoPlay: true);
    switch (index) {
      case 0: //封面图
        return model.contentBean.video == null
            ? CachedNetworkImage(
                imageUrl: ImageUrlUtil.getClean(model.contentBean.cover),
                width: 750.w,
                height: 550.h,
                placeholder: (_, __) => Constants.loadingIcon(),
                fit: BoxFit.cover,
              )
            : Container(
                height: 400,
                child: IjkPlayer(
                  mediaController: controller,
//                  controllerWidgetBuilder: (ctl) => DefaultIJKControllerWidget(
//                    controller: ctl,
//                    showFullScreenButton: false,
//                  ),
                ));
      case 1: //个人信息
        return Container(
          padding: EdgeInsets.only(top: 31.h, left: 37.w, right: 36.w),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              GestureDetector(
                  onTap: () {
                    Navigator.of(context).pushNamed(Routes.mine_user_info,
                        arguments: model.contentBean.user.userId.toString());
                  },
                  child: headClipRRect(model.contentBean.user.portrait, 83.w)),
              Expanded(
                child: Padding(
                  padding: EdgeInsets.only(left: 20.w),
                  child: Text(
                    model.contentBean.user.nickname,
                    style: TextStyle(fontSize: 29.sp, fontWeight: FontWeight.w600),
                  ),
                ),
              ),
              Selector<StudyDetailViewModel, int>(
                builder: (context, data, __) {
                  return Container(
                    margin: EdgeInsets.only(top: 9.h),
                    width: 125.w,
                    height: 49.h,
                    decoration: new BoxDecoration(
                      border: new Border.all(color: data == 1 ? blue : grey, width: 1.w),
                      borderRadius: BorderRadius.circular(4.w),
                    ),
                    child: FlatButton(
                        padding: EdgeInsets.all(0),
                        onPressed: () {
                          if (isLogin) {
                            Navigator.of(context).pushNamed(Routes.login_page);
                          } else {
                            if (data == 0) {
                              context
                                  .read<StudyDetailViewModel>()
                                  .followThisUser(onError: (msg) => Toast.show(msg, context));
                            } else {
                              context
                                  .read<StudyDetailViewModel>()
                                  .unFollowThisUser(onError: (msg) => Toast.show(msg, context));
                            }
                          }
                        },
                        child: Text(
                          data == 1 ? '已关注' : '关注',
                          maxLines: 1,
                          style: TextStyle(fontSize: 23.sp, color: data == 1 ? blue : grey),
                        )),
                  );
                },
                selector: (_, model) => model.contentBean.user.isFollowed,
              )
            ],
          ),
        );
      case 2: //文章标题 阅读 点赞 评论 时间展示
        return Padding(
          padding: EdgeInsets.only(left: 37.w, right: 36.w, top: 45.h, bottom: 40.h),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Container(
                width: 537.w,
                child: Text(
                  model.contentBean.title,
                  maxLines: 2,
                  style: TextStyle(fontSize: 30.sp, fontWeight: FontWeight.w800),
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: 32.h),
                child: Row(
                  children: <Widget>[
                    Expanded(
                        child: Text(
                      FormatTime.readTimestamp(model.contentBean.add_time),
                      style: TextStyle(
                          color: Color(0xFF999999), fontSize: 22.sp, fontWeight: FontWeight.w500),
                    )),
                    Builder(builder: (context) {
                      var data = context
                          .select<StudyDetailViewModel, int>((value) => value.contentBean.view_num);
                      return tag('images/eye.png', "$data", 32.w, 24.w, 22.sp);
                    }),
                    Builder(builder: (context) {
                      var data = context
                          .select<StudyDetailViewModel, int>((value) => value.contentBean.like_num);
                      return Padding(
                        padding: EdgeInsets.only(left: 29.w),
                        child: tag('images/like.png', "$data", 30.w, 30.w, 22.sp),
                      );
                    }),
                    Builder(builder: (context) {
                      if (Provider.of<StudyDetailViewModel>(context, listen: false)
                              .currentCategory ==
                          1) {
                        return Constants.empty;
                      }
                      var data = context.select<StudyDetailViewModel, int>(
                          (value) => value.contentBean.comment_num);
                      return Padding(
                        padding: EdgeInsets.only(left: 29.w),
                        child: tag('images/comment.png', "$data", 30.w, 30.w, 22.sp),
                      );
                    }),
                  ],
                ),
              )
            ],
          ),
        );
      case 3:
        return Divider(height: 1);
      case 4: //文章内容
        return createContent(model.contentBean.contentList, onClick: (src) {
          List<String> list =
              model.contentBean.contentList.where((element) => element.startsWith('http')).toList();
          Navigator.push(
            context,
            new MaterialPageRoute(
                builder: (context) => PhotoViewSimpleScreen(
                      imagePath: src,
                      images: list,
                      heroTag: 'simple',
                    )),
          );
        }, onLongClick: (src) async {
          if (context.read<LoginViewModel>().isLogin()) {
            Navigator.of(context).pushNamed(Routes.login_page);
            return;
          }
          bool success = await showDialog(context: context, child: BoardDialog(path: src));
          if (success!=null && success) {
            Toast.show("收藏完成", context);
          }
        });
      case 5: //评论内容
        return Builder(builder: (context) {
          var comments =
              context.select<StudyDetailViewModel, List<Comment>>((value) => value.comments);
          if (comments != null && comments.length > 0) {
            return _createComment(context, comments);
          } else {
            return Constants.empty;
          }
        });
      case 6:
        return Container(
          margin: EdgeInsets.only(top: 31.h, left: 37.w, right: 36.w),
          padding: EdgeInsets.only(top: 8.h, bottom: 30.h),
          decoration: BoxDecoration(
              border: Border(
            top: BorderSide(color: Color(0xFFEBECF1), width: 0.5.w),
          )),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              _boldText('注：'),
              _thickText('会员发布的资料仅限於个人测试学习之用,不得用於商业用途或向外传播并请在下载后24小时内删除。'),
              _boldText('免责声明'),
              _thickText(
                  '以上图片及资料内容来源于网络，由设计网整理，版权归原作者及其公司所有；本资料仅供学习研究之用，不得用作于商业用途若此图库侵犯到您的权益，请与我们联系删除。'),
              _boldText('关于部分资料页面无链接问题'),
              _thickText('资料页面最新的资料目前是向全网人员免费开放的，在此之前的有部分资料是合集类型，属于精品资料，需要下载app才能获得。')
            ],
          ),
        );
      case 7:
        return Selector<StudyDetailViewModel, List<StudyInfo>>(
            builder: (context, data, __) {
              if (data != null && data.length > 0) {
                return Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Container(
                      height: 20.h,
                      color: Color(0xFFF2F2F2),
                    ),
                    Padding(
                      padding: EdgeInsets.only(left: 35.w, top: 60.h, bottom: 40.h),
                      child: Text(
                        '猜你喜欢',
                        style: TextStyle(fontSize: 30.sp, fontWeight: FontWeight.bold),
                      ),
                    ),
                    _getLikeItem(context, data)
                  ],
                );
              } else {
                return Text('');
              }
            },
            selector: (_, model) => model.likeStudy);
    }
    return Text('index:$index');
  }

  Widget createContent(List<dynamic> content,
      {Function(String url) onClick, Function(String url) onLongClick}) {
    if (content != null && content.length > 0) {
      return Padding(
        padding: EdgeInsets.only(top: 50.h, bottom: 30.h),
        child: ListView.builder(
          itemCount: content.length,
          shrinkWrap: true,
          physics: NeverScrollableScrollPhysics(),
          itemBuilder: (_, index) {
            var e = content[index];
            if (e.startsWith('http')) {
              return GestureDetector(
                onTap: () {
                  onClick.call(e);
                },
                onLongPress: () {
                  onLongClick.call(e);
                },
                child: CachedNetworkImage(
                  imageUrl: ImageUrlUtil.getClean(e),
                  width: 750.w,
                  // height: e.ratio == 0 ? 0 : 750.w / e.ratio,
                  fit: BoxFit.cover,
                ),
              );
            } else {
              return Padding(
                padding: EdgeInsets.only(left: 30.w, right: 30.w, bottom: 10.h, top: 10.h),
                child: Text(
                  e,
                  style: TextStyle(fontSize: 24.sp, color: Colors.black),
                ),
              );
            }
          },
        ),
      );
    } else {
      return Constants.empty;
    }
  }

  // 猜你喜欢
  Widget _getLikeItem(BuildContext context, List<StudyInfo> list) {
    List<Widget> widgets = list.map((entity) {
      if (entity.type != null) {
        if (entity.type == '1') {
          return _StudyItemWidget1(
            entity: entity,
            isLogin: isLogin,
          );
        }
        if (entity.type == '2') {
          return _StudyItemWidget2(
            entity: entity,
            isLogin: isLogin,
          );
        }
      } else {
        return _StudyItemWidget2(
          entity: entity,
          isLogin: isLogin,
        );
      }
      return Text('');
    }).toList();
    return Padding(
      padding: EdgeInsets.only(left: 36.w, right: 36.w),
      child: Column(
        children: widgets,
      ),
    );
  }

  //评论列表
  Widget _createComment(BuildContext context, List<Comment> comment) {
    List<Widget> list = [
      Text(
        '评论(${context.watch<StudyDetailViewModel>().commentCount})',
        style: TextStyle(fontSize: 30.sp, fontWeight: FontWeight.bold),
      ),
      Padding(
        padding: EdgeInsets.only(top: 18.h, bottom: 48.h),
        child: Divider(
          height: 1,
        ),
      )
    ];
    for (int i = 0; i < comment.length; i++) {
      list.add(_commentItem(context, comment[i], model));
      if (i < comment.length - 1) {
        list.add(Padding(
          padding: EdgeInsets.only(top: 30.h, bottom: 46.h),
          child: Divider(
            height: 1,
          ),
        ));
      } else {
        list.add(Builder(builder: (context) {
          var data = context.watch<StudyDetailViewModel>().likeStudy;
          if (data == null || data.length == 0) {
            return Padding(
              padding: EdgeInsets.only(top: 30.h, bottom: 200.h),
              child: Divider(
                height: 1,
              ),
            );
          } else {
            return Padding(
              padding: EdgeInsets.only(top: 30.h),
              child: Divider(
                height: 1,
              ),
            );
          }
        }));
      }
    }
    if (model.isNextComment()) {
      list.removeLast();
      list.add(Padding(
        padding: EdgeInsets.only(top: 30.h, bottom: 30.h),
        child: Divider(
          height: 1,
        ),
      ));
      list.add(Container(
        padding: EdgeInsets.only(bottom: 40.h),
        width: double.infinity,
        child: Center(
          child: InkWell(
            onTap: () {
              model.nextComment();
            },
            child: Text(
              '查看更多评论',
              style: TextStyle(fontSize: 20.sp, color: Color(0xFF999999)),
            ),
          ),
        ),
      ));
    }
    return Padding(
      padding: EdgeInsets.only(left: 37.w, top: 90.h),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: list,
      ),
    );
  }

  //一级品论样式
  Widget _commentItem(BuildContext context, Comment comment, StudyDetailViewModel model) {
    return Column(
      children: <Widget>[
        Row(
          children: <Widget>[
            GestureDetector(
                onTap: () {
                  Navigator.of(context)
                      .pushNamed(Routes.mine_user_info, arguments: comment.user.userId.toString());
                },
                child: headClipRRect(comment.user.portrait, 72.w)),
            Expanded(
              child: Padding(
                padding: EdgeInsets.only(left: 18.w, right: 30.w),
                child: Text(
                  comment.user.nickname,
                  style: TextStyle(fontSize: 28.sp, fontWeight: FontWeight.bold),
                ),
              ),
            ), //评论头像 //昵称
            Builder(
              builder: (BuildContext context) {
                var info = Provider.of<LoginViewModel>(context, listen: false).info;
                var content = context.watch<StudyDetailViewModel>().contentBean;
                List<PopupMenuEntry<int>> listWidget = [];
                if ((info != null && info?.user?.userId == content.user.userId) ||
                    (info?.user?.userId == comment.user.userId)) {
                  listWidget.add(PopupMenuItem(
                      value: 0,
                      child: Row(
                        children: <Widget>[
                          Image.asset(
                            'images/trash_can.png',
                            width: 30.w,
                            height: 30.w,
                          ),
                          Padding(
                            padding: EdgeInsets.only(left: 17.w),
                            child: Text(
                              '删除评论',
                              style: TextStyle(
                                fontSize: 28.sp,
                                fontWeight: FontWeight.w600,
                                color: Color(0xFF696C72),
                              ),
                            ),
                          )
                        ],
                      )));
                } else {
                  listWidget.add(PopupMenuItem(
                      value: 1,
                      child: Row(
                        children: <Widget>[
                          Image.asset(
                            'images/report.png',
                            width: 30.w,
                            height: 30.w,
                          ),
                          Padding(
                            padding: EdgeInsets.only(left: 17.w),
                            child: Text(
                              '举报评论',
                              style: TextStyle(
                                fontSize: 28.sp,
                                fontWeight: FontWeight.w600,
                                color: Color(0xFF696C72),
                              ),
                            ),
                          )
                        ],
                      )));
                }
                return Padding(
                    padding: EdgeInsets.only(right: 36.w),
                    child: Material(
                      child: PopupMenuButton<int>(
                        onSelected: (index) {
                          if (index == 0) {
                            context.read<StudyDetailViewModel>().deleteCommentNotify(comment,
                                onError: (msg) => Toast.show(msg, context));
                          } else {
                            Navigator.of(context).pushNamed(Routes.report_page, arguments: {
                              'id': comment.id.toString(),
                              'reportType': ReportType.COMMENT
                            });
                          }
                        },
                        itemBuilder: (BuildContext context) {
                          return listWidget;
                        },
                        child: Text(
                          '···',
                          style: TextStyle(
                              letterSpacing: 5.w,
                              color: Color(0xFF999999),
                              fontSize: 33.sp,
                              fontWeight: FontWeight.bold),
                        ),
                      ),
                    ));
              },
            )
          ],
        ),
        Container(
          width: double.infinity,
          child: Padding(
            padding: EdgeInsets.only(left: 90.w, top: 13.h, right: 36.w),
            child: TextField(
              minLines: 1,
              maxLines: 50,
              readOnly: true,
              style: TextStyle(
                fontWeight: FontWeight.w600,
                fontSize: 24.sp,
                color: Theme.of(context).textTheme.subtitle2.color,
              ),
              decoration: InputDecoration(
                contentPadding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
                border: OutlineInputBorder(
                  borderSide: BorderSide(color: Colors.white, width: 0),
                ),
                focusedBorder: OutlineInputBorder(
                    borderSide: BorderSide(color: Colors.white, width: 0),
                    borderRadius: BorderRadius.all(Radius.circular(0))),
                enabledBorder: OutlineInputBorder(
                    borderSide: BorderSide(color: Colors.white, width: 0),
                    borderRadius: BorderRadius.all(Radius.circular(0))),
              ),
              controller: TextEditingController(text: comment.content),
            ),
          ),
        ), //评论内容
        Container(
          margin: EdgeInsets.only(left: 90.w),
          child: Row(
            children: <Widget>[
              Expanded(
                child: Text(
                  FormatTime.readTimestamp(comment.add_time),
                  style: TextStyle(
                      color: Color(0xFF999999), fontSize: 20.sp, fontWeight: FontWeight.w500),
                ),
              ), //评论时间
              Padding(
                padding: EdgeInsets.only(right: 5.w),
                child: SizedBox(
                  width: 72.w,
                  child: FlatButton(
                    padding: EdgeInsets.all(1.w),
                    onPressed: () {
                      if (isLogin) {
                        Navigator.of(context).pushNamed(Routes.login_page);
                      } else {
                        commentIsHide.value = CommentValueData(false,
                            '回复:@${comment.user.nickname}', CommentType.COMMENT_TO_USER, comment);
                        _focusScope.requestFocus(_commentFocus);
                      }
                    },
                    child: Image.asset(
                      'images/comment.png',
                      width: 26.w,
                      height: 26.w,
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
              ),
              Selector<StudyDetailViewModel, int>(
                builder: (context, data, ___) {
                  return Padding(
                    padding: EdgeInsets.only(right: 50.w),
                    child: SizedBox(
                      width: 72.w,
                      child: FlatButton(
                        padding: EdgeInsets.all(1.w),
                        onPressed: () {
                          if (isLogin) {
                            Navigator.of(context).pushNamed(Routes.login_page);
                          } else {
                            if (data == 0) {
                              model.likeComment(comment);
                            } else {
                              model.unLikeComment(comment);
                            }
                          }
                        },
                        child: Stack(
                          alignment: Alignment.centerLeft,
                          children: <Widget>[
                            Image.asset(
                              data == 0 ? "images/like.png" : "images/like_active.png",
                              width: 25.w,
                              height: 25.w,
                              fit: BoxFit.cover,
                            ),
                            Padding(
                              padding: EdgeInsets.only(
                                left: 37.w,
                                top: 4.h,
                              ),
                              child: Text(
                                '${comment.likes_num}',
                                style: TextStyle(
                                    fontSize: 17.sp,
                                    color: data == 0 ? Color(0xFF999999) : Color(0xFF0CABCB)),
                              ),
                            )
                          ],
                        ),
                      ),
                    ),
                  );
                },
                selector: (_, __) => comment.is_like,
                shouldRebuild: (old, next) => old != next,
              ) //评论点赞
            ],
          ),
        ),
        Selector<StudyDetailViewModel, List<Comment>>(
            builder: (_, data, __) {
              if (data != null && data.length > 0) {
                return _subComment(comment, model);
              } else {
                return Text('');
              }
            },
            selector: (_, __) => comment.sub_comment)
      ],
    );
  }

  //二级评论
  Widget _subComment(Comment comment, StudyDetailViewModel model) {
    var subComment = comment.sub_comment;
    List<Widget> list = [];
    for (int i = 0; i < subComment.length; i++) {
      Comment sub = subComment[i];
      list.add(Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              GestureDetector(
                onTap: () {
                  Navigator.of(context)
                      .pushNamed(Routes.mine_user_info, arguments: sub.user.userId.toString());
                },
                child: headClipRRect(sub.user.portrait, 72.w),
              ),
              Expanded(
                child: Padding(
                  padding: EdgeInsets.only(left: 18.w, right: 30.w),
                  child: Text(
                    sub.user.nickname,
                    style: TextStyle(fontSize: 28.sp, fontWeight: FontWeight.bold),
                  ),
                ),
              ), //评论头像 //昵称
              Builder(builder: (context) {
                var info = Provider.of<LoginViewModel>(context, listen: false).info;
                var content = Provider.of<StudyDetailViewModel>(context, listen: false).contentBean;
                List<PopupMenuEntry<int>> listWidget = [];
                if ((info != null && info?.user?.userId == content.user.userId) ||
                    (info?.user?.userId == sub.user.userId)) {
                  listWidget.add(PopupMenuItem(
                      value: 0,
                      child: Row(
                        children: <Widget>[
                          Image.asset(
                            'images/trash_can.png',
                            width: 30.w,
                            height: 30.w,
                          ),
                          Padding(
                            padding: EdgeInsets.only(left: 17.w),
                            child: Text(
                              '删除评论',
                              style: TextStyle(
                                fontSize: 28.sp,
                                fontWeight: FontWeight.w600,
                                color: Color(0xFF696C72),
                              ),
                            ),
                          )
                        ],
                      )));
                } else {
                  listWidget.add(PopupMenuItem(
                      value: 1,
                      child: Row(
                        children: <Widget>[
                          Image.asset(
                            'images/report.png',
                            width: 30.w,
                            height: 30.w,
                          ),
                          Padding(
                            padding: EdgeInsets.only(left: 17.w),
                            child: Text(
                              '举报评论',
                              style: TextStyle(
                                fontSize: 28.sp,
                                fontWeight: FontWeight.w600,
                                color: Color(0xFF696C72),
                              ),
                            ),
                          )
                        ],
                      )));
                }

                return Padding(
                    padding: EdgeInsets.only(right: 36.w),
                    child: PopupMenuButton<int>(
                      onSelected: (index) {
                        if (index == 0) {
                          context.read<StudyDetailViewModel>().deleteCommentNotify(sub,
                              onError: (msg) => Toast.show(msg, context),
                              isSub: true,
                              params: comment);
                        } else {
                          Navigator.of(context).pushNamed(Routes.report_page, arguments: {
                            'id': sub.id.toString(),
                            'reportType': ReportType.COMMENT
                          });
                        }
                      },
                      itemBuilder: (BuildContext context) {
                        return listWidget;
                      },
                      child: Text(
                        '···',
                        style: TextStyle(
                            letterSpacing: 5.w,
                            color: Color(0xFF999999),
                            fontSize: 33.sp,
                            fontWeight: FontWeight.bold),
                      ),
                    ));
              })
            ],
          ),
          Container(
            width: double.infinity,
            child: Padding(
                padding: EdgeInsets.only(left: 90.w, top: 13.h, right: 36.w),
                child: TextField(
                  minLines: 1,
                  maxLines: 50,
                  readOnly: true,
                  style: TextStyle(
                    fontWeight: FontWeight.w600,
                    fontSize: 24.sp,
                    color: Theme.of(context).textTheme.subtitle2.color,
                  ),
                  decoration: InputDecoration(
                    contentPadding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
                    border: OutlineInputBorder(
                      borderSide: BorderSide(color: Colors.transparent, width: 0),
                    ),
                    focusedBorder: OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.transparent, width: 0),
                        borderRadius: BorderRadius.all(Radius.circular(0))),
                    enabledBorder: OutlineInputBorder(
                        borderSide: BorderSide(color: Colors.transparent, width: 0),
                        borderRadius: BorderRadius.all(Radius.circular(0))),
                  ),
                  controller: TextEditingController(text: sub.content),
                )),
          ), //评论内容
          Container(
            margin: EdgeInsets.only(left: 90.w),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Expanded(
                  child: Text(
                    FormatTime.readTimestamp(sub.add_time),
                    style: TextStyle(
                        color: Color(0xFF999999), fontSize: 20.sp, fontWeight: FontWeight.w500),
                  ),
                ), //评论时间
                Padding(
                  padding: EdgeInsets.only(right: 5.w),
                  child: SizedBox(
                    width: 72.w,
                    child: FlatButton(
                      padding: EdgeInsets.all(1.w),
                      onPressed: () {
                        if (isLogin) {
                          Navigator.of(context).pushNamed(Routes.login_page);
                        } else {
                          commentIsHide.value = CommentValueData(
                              false, '回复:@${sub.user.nickname}', CommentType.COMMENT_TO_USER, sub,
                              params: comment);
                          _focusScope.requestFocus(_commentFocus);
                        }
                      },
                      child: Image.asset(
                        'images/comment.png',
                        width: 26.w,
                        height: 26.w,
                        fit: BoxFit.cover,
                      ),
                    ),
                  ),
                ),
                Selector<StudyDetailViewModel, int>(
                  builder: (context, data, ___) {
                    return Padding(
                      padding: EdgeInsets.only(right: 50.w),
                      child: SizedBox(
                        width: 72.w,
                        child: FlatButton(
                          padding: EdgeInsets.all(1.w),
                          onPressed: () {
                            if (isLogin) {
                              Navigator.of(context).pushNamed(Routes.login_page);
                            } else {
                              if (data == 0) {
                                model.likeComment(sub);
                              } else {
                                model.unLikeComment(sub);
                              }
                            }
                          },
                          child: Stack(
                            alignment: Alignment.centerLeft,
                            children: <Widget>[
                              Image.asset(
                                data == 0 ? "images/like.png" : "images/like_active.png",
                                width: 25.w,
                                height: 25.w,
                                fit: BoxFit.cover,
                              ),
                              Padding(
                                padding: EdgeInsets.only(
                                  left: 37.w,
                                  top: 4.h,
                                ),
                                child: Text(
                                  '${sub.likes_num}',
                                  style: TextStyle(
                                      fontSize: 17.sp,
                                      color: data == 0 ? Color(0xFF999999) : Color(0xFF0CABCB)),
                                ),
                              )
                            ],
                          ),
                        ),
                      ),
                    );
                  },
                  selector: (_, __) => sub.is_like,
                  shouldRebuild: (old, next) => old != next,
                ) //评论点赞
              ],
            ),
          ),
        ],
      ));
      if (i < subComment.length - 1) {
        list.add(Divider(height: 1));
      }
    }
    if (model.isNextSubComment(comment)) {
      list.add(Container(
        padding: EdgeInsets.only(bottom: 40.h),
        width: double.infinity,
        child: Center(
          child: InkWell(
            onTap: () {
              model.nextSubComment(comment);
            },
            child: Text(
              '更多${comment.sub_comment_num - comment.sub_comment.length}条回复',
              style: TextStyle(fontSize: 20.sp, color: Color(0xFF999999)),
            ),
          ),
        ),
      ));
    }
    return Stack(
      children: <Widget>[
        Container(
          margin: EdgeInsets.only(
            top: 41.h,
            left: 90.w,
            right: 36.w,
          ),
          color: Color(0xFFF6F6F8),
          width: double.infinity,
          child: Padding(
            padding: EdgeInsets.only(left: 37.w, top: 37.h),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: list,
            ),
          ),
        ),
        Positioned(
            top: 22.h,
            left: 150.w,
            child: CustomPaint(
              painter: TrianglePainter(
                strokeColor: Color(0xFFEFF0F4),
                strokeWidth: 24.w,
                paintingStyle: PaintingStyle.fill,
              ),
            ))
      ],
    );
  }

  //小tag数字标记
  Widget tag(String path, String text, double imageW, double imageH, double textS) {
    return Row(
      children: <Widget>[
        Image.asset(
          path,
          width: imageW,
          height: imageH,
        ),
        Padding(
          padding: EdgeInsets.only(left: 5.w),
          child: Text(
            text,
            style: TextStyle(fontSize: textS, color: grey),
          ),
        )
      ],
    );
  }

  //头像
  ClipRRect headClipRRect(String url, double circular) {
    return ClipRRect(
      borderRadius: BorderRadius.circular(circular),
      child: CachedNetworkImage(
        imageUrl: ImageUrlUtil.getClean(url),
        height: circular,
        width: circular,
        fit: BoxFit.cover,
      ),
    );
  }

//  Widget warn() {
//
//  }

  _thickText(text) {
    return Text(
      text,
      style: TextStyle(
        color: Color(0xFF999999),
        fontSize: 22.sp,
        fontWeight: FontWeight.w500,
      ),
    );
  }

  _boldText(text) {
    return Text(
      text,
      style: TextStyle(
          color: Color(0xFF82868E), fontSize: 24.sp, fontWeight: FontWeight.w500, height: 4),
    );
  }
}

class _StudyItemWidget1 extends StatelessWidget {
  final StudyInfo entity;
  final bool isLogin;

  const _StudyItemWidget1({Key key, this.entity, this.isLogin}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        int category = Provider.of<StudyDetailViewModel>(context, listen: false).currentCategory;
        Navigator.of(context).pushNamed(Routes.study_detail,
            arguments: {"detail": entity, "currentCategory": category}).then((value) {
          context.read<StudyDetailViewModel>().changeEntity();
        });
      },
      child: Container(
        decoration: BoxDecoration(
          color: Colors.white,
        ),
        margin: EdgeInsets.only(bottom: 30.h),
        width: double.infinity,
        height: 230.h,
        padding: EdgeInsets.all(20.w),
        child: Row(
          mainAxisSize: MainAxisSize.max,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(right: 28.w),
              child: Stack(
                alignment: Alignment.center,
                children: <Widget>[
                  Container(
                      width: 189.w,
                      height: 189.h,
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(5.w),
                        child: Image.network(
                          entity.cover,
                          fit: BoxFit.cover,
                        ),
                      )),
                  Image.asset('images/study_video.png', width: 60.w, height: 60.h),
                ],
              ),
            ),
            Expanded(
              flex: 1,
              child: Column(
                children: <Widget>[
                  Row(
                    children: <Widget>[
                      Expanded(
                          flex: 1,
                          child: Text(
                            entity.title,
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(
                              color: Color(0xFF000000),
                              fontSize: 28.sp,
                              fontWeight: FontWeight.w500,
                            ),
                          )),
                      Container(
                        margin: EdgeInsets.only(left: 20.w),
                        padding: EdgeInsets.fromLTRB(13.w, 0, 13.w, 0),
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.all(Radius.circular(5.w)),
                            border: Border.all(color: Color(0xFF82868E), width: 1.w)),
                        child: Text(
                          '视频',
                          style: TextStyle(
                            color: Color(0xFF82868E),
                            fontSize: 21.sp,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                      )
                    ],
                  ),
                  Expanded(
                    flex: 2,
                    child: Container(
                      padding: EdgeInsets.only(top: 20.h),
                      alignment: Alignment.topLeft,
                      child: Text(
                        entity.brief,
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                          color: Color(0xFF82868E),
                          fontSize: 22.sp,
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                    ),
                  ),
                  Expanded(
                    flex: 1,
                    child: Row(
                      children: <Widget>[
                        Expanded(
                            flex: 1,
                            child: Row(
                              children: <Widget>[
                                Container(
                                  margin: EdgeInsets.only(right: 13.w),
                                  child: CircleAvatar(
                                    radius: 13,
                                    backgroundImage: NetworkImage(entity.user.portrait),
                                  ),
                                ),
                                Expanded(
                                  flex: 1,
                                  child: Text(
                                    entity.user.nickname,
                                    maxLines: 1,
                                    overflow: TextOverflow.ellipsis,
                                    style: TextStyle(
                                      color: Color(0xFF82868E),
                                      fontSize: 24.sp,
                                      fontWeight: FontWeight.w500,
                                    ),
                                  ),
                                )
                              ],
                            )),
                        Row(
                          children: <Widget>[
                            Selector<StudyDetailViewModel, int>(
                              builder: (_, data, ___) {
                                return Container(
                                  margin: EdgeInsets.only(left: 50.w),
                                  padding: EdgeInsets.only(right: 20.w),
                                  child: SizedBox(
                                    width: 72.w,
                                    child: FlatButton(
                                      padding: EdgeInsets.all(1.w),
                                      onPressed: () {
                                        if (isLogin) {
                                          Navigator.of(context).pushNamed(Routes.login_page);
                                        } else {
                                          if (data == 0) {
                                            context
                                                .read<StudyDetailViewModel>()
                                                .likeToLikeStudy(entity);
                                          } else {
                                            context
                                                .read<StudyDetailViewModel>()
                                                .unLikeToLikeStudy(entity);
                                          }
                                        }
                                      },
                                      child: Stack(
                                        alignment: Alignment.centerLeft,
                                        children: <Widget>[
                                          Image.asset(
                                            entity.is_like == 0
                                                ? "images/like.png"
                                                : "images/like_active.png",
                                            width: 30.w,
                                            height: 30.w,
                                            fit: BoxFit.cover,
                                          ),
                                          Padding(
                                            padding: EdgeInsets.only(
                                              left: 37.w,
                                              top: 4.h,
                                            ),
                                            child: Text(
                                              '${entity.like_num}',
                                              style: TextStyle(
                                                  fontSize: 22.sp,
                                                  color: entity.is_like == 0
                                                      ? Color(0xFF999999)
                                                      : Color(0xFF0CABCB)),
                                            ),
                                          )
                                        ],
                                      ),
                                    ),
                                  ),
                                );
                              },
                              selector: (_, __) => entity.is_like,
                            ),
                            Selector<StudyDetailViewModel, int>(
                              builder: (context, __, ___) {
                                return SizedBox(
                                  width: 72.w,
                                  child: FlatButton(
                                    padding: EdgeInsets.all(1.w),
                                    onPressed: () {
                                      if (isLogin) {
                                        Navigator.of(context).pushNamed(Routes.login_page);
                                        return;
                                      }
                                      int type =
                                          context.read<StudyDetailViewModel>().currentCategory;
                                      if (entity.is_collect == 0) {
                                        FileType fileType;
                                        if (type == 0) {
                                          fileType = FileType.KNOWLEDGE;
                                        } else {
                                          fileType = FileType.APPLY;
                                        }
                                        Navigator.of(context).pushNamed(Routes.master_file_add,
                                            arguments: {
                                              'type': fileType,
                                              'id': entity.id.toString()
                                            }).then((value) {
                                          if (value) {
                                            entity.is_collect = 1;
                                            entity.collect_num += 1;
                                            context.read<StudyDetailViewModel>().changeEntity();
                                          }
                                        });
                                      } else {
                                        context
                                            .read<StudyDetailViewModel>()
                                            .unLikeCollection(entity);
                                      }
                                    },
                                    child: Stack(
                                      alignment: Alignment.centerLeft,
                                      children: <Widget>[
                                        Image.asset(
                                          entity.is_collect == 0
                                              ? "images/collect.png"
                                              : "images/collected.png",
                                          width: 30.w,
                                          height: 30.w,
                                          fit: BoxFit.cover,
                                        ),
                                        Padding(
                                          padding: EdgeInsets.only(
                                            left: 37.w,
                                            top: 4.h,
                                          ),
                                          child: Text(
                                            '${entity.collect_num}',
                                            style: TextStyle(
                                                fontSize: 22.sp,
                                                color: entity.is_collect == 0
                                                    ? Color(0xFF999999)
                                                    : Color(0xFF0CABCB)),
                                          ),
                                        )
                                      ],
                                    ),
                                  ),
                                );
                              },
                              selector: (_, __) => entity.is_collect,
                              shouldRebuild: (old, next) => old != next,
                            )
                          ],
                        )
                      ],
                    ),
                  )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}

class _StudyItemWidget2 extends StatelessWidget {
  final StudyInfo entity;
  final bool isLogin;
  static const Widget empty = const SizedBox.shrink();

  const _StudyItemWidget2({
    Key key,
    this.entity,
    this.isLogin,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        int category = Provider.of<StudyDetailViewModel>(context, listen: false).currentCategory;
        Navigator.of(context).pushNamed(Routes.study_detail,
            arguments: {"detail": entity, "currentCategory": category}).then((value) {
          context.read<StudyViewModel>().changeEntity();
        });
      },
      child: Column(
        children: <Widget>[
          Container(
            width: double.infinity,
            height: 350.h,
            child: Image.network(
              entity.cover,
              fit: BoxFit.cover,
            ),
          ),
          Container(
            margin: EdgeInsets.only(bottom: 30.h),
            decoration: BoxDecoration(
              color: Colors.white,
            ),
            width: double.infinity,
            padding: EdgeInsets.all(30.w),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    Expanded(
                        child: Text(
                      entity.title,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                        color: Color(0xFF000000),
                        fontSize: 28.sp,
                        fontWeight: FontWeight.w500,
                      ),
                    )),
                    entity.type != null
                        ? Container(
                            padding: EdgeInsets.fromLTRB(13.w, 0, 13.w, 0),
                            decoration: BoxDecoration(
                                borderRadius: BorderRadius.all(Radius.circular(2.5)),
                                border: Border.all(color: Color(0xFF82868E), width: 0.5)),
                            child: Text(
                              '图文',
                              style: TextStyle(
                                color: Color(0xFF82868E),
                                fontSize: 21.sp,
                                fontWeight: FontWeight.w500,
                              ),
                            ),
                          )
                        : empty
                  ],
                ),
                Container(
                  margin: const EdgeInsets.only(top: 9.5, bottom: 14),
                  child: Text(
                    entity.brief,
                    maxLines: 1,
                    style: TextStyle(
                      color: Color(0xFF82868E),
                      fontSize: 22.sp,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                ),
                Row(
                  children: <Widget>[
                    Expanded(
                      child: Row(
                        children: <Widget>[
                          Expanded(
                            child: Row(
                              children: <Widget>[
                                Container(
                                  margin: EdgeInsets.only(right: 13.w),
                                  child: CircleAvatar(
                                    radius: 13,
                                    backgroundImage: NetworkImage(entity.user.portrait),
                                  ),
                                ),
                                Text(
                                  entity.user.nickname,
                                  style: TextStyle(
                                    color: Color(0xFF82868E),
                                    fontSize: 24.sp,
                                    fontWeight: FontWeight.w500,
                                  ),
                                )
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                    Row(
                      children: <Widget>[
                        Selector<StudyDetailViewModel, int>(
                          builder: (_, data, ___) {
                            return Container(
                              margin: EdgeInsets.only(left: 50.w),
                              padding: EdgeInsets.only(right: 20.w),
                              child: SizedBox(
                                width: 72.w,
                                child: FlatButton(
                                  padding: EdgeInsets.all(1.w),
                                  onPressed: () {
                                    if (isLogin) {
                                      Navigator.of(context).pushNamed(Routes.login_page);
                                    } else {
                                      if (data == 0) {
                                        context
                                            .read<StudyDetailViewModel>()
                                            .likeToLikeStudy(entity);
                                      } else {
                                        context
                                            .read<StudyDetailViewModel>()
                                            .unLikeToLikeStudy(entity);
                                      }
                                    }
                                  },
                                  child: Stack(
                                    alignment: Alignment.centerLeft,
                                    children: <Widget>[
                                      Image.asset(
                                        entity.is_like == 0
                                            ? "images/like.png"
                                            : "images/like_active.png",
                                        width: 30.w,
                                        height: 30.w,
                                        fit: BoxFit.cover,
                                      ),
                                      Padding(
                                        padding: EdgeInsets.only(
                                          left: 37.w,
                                          top: 4.h,
                                        ),
                                        child: Text(
                                          '${entity.like_num}',
                                          style: TextStyle(
                                              fontSize: 22.sp,
                                              color: entity.is_like == 0
                                                  ? Color(0xFF999999)
                                                  : Color(0xFF0CABCB)),
                                        ),
                                      )
                                    ],
                                  ),
                                ),
                              ),
                            );
                          },
                          selector: (_, __) => entity.is_like,
                        ),
                        Selector<StudyDetailViewModel, int>(
                          builder: (_, __, ___) {
                            return SizedBox(
                              width: 72.w,
                              child: FlatButton(
                                padding: EdgeInsets.all(1.w),
                                onPressed: () {
                                  if (isLogin) {
                                    Navigator.of(context).pushNamed(Routes.login_page);
                                    return;
                                  }
                                  int type = context.read<StudyDetailViewModel>().currentCategory;
                                  if (entity.is_collect == 0) {
                                    FileType fileType;
                                    if (type == 0) {
                                      fileType = FileType.KNOWLEDGE;
                                    } else {
                                      fileType = FileType.APPLY;
                                    }
                                    Navigator.of(context).pushNamed(Routes.master_file_add,
                                        arguments: {
                                          'type': fileType,
                                          'id': entity.id.toString()
                                        }).then((value) {
                                      if (value) {
                                        entity.is_collect = 1;
                                        entity.collect_num += 1;
                                        context.read<StudyDetailViewModel>().changeEntity();
                                      }
                                    });
                                  } else {
                                    context.read<StudyDetailViewModel>().unLikeCollection(entity);
                                  }
                                },
                                child: Stack(
                                  alignment: Alignment.centerLeft,
                                  children: <Widget>[
                                    Image.asset(
                                      entity.is_collect == 0
                                          ? "images/collect.png"
                                          : "images/collected.png",
                                      width: 30.w,
                                      height: 30.w,
                                      fit: BoxFit.cover,
                                    ),
                                    Padding(
                                      padding: EdgeInsets.only(
                                        left: 37.w,
                                        top: 4.h,
                                      ),
                                      child: Text(
                                        '${entity.collect_num}',
                                        style: TextStyle(
                                            fontSize: 22.sp,
                                            color: entity.is_collect == 0
                                                ? Color(0xFF999999)
                                                : Color(0xFF0CABCB)),
                                      ),
                                    )
                                  ],
                                ),
                              ),
                            );
                          },
                          selector: (_, __) => entity.is_collect,
                          shouldRebuild: (old, next) => old != next,
                        )
                      ],
                    )
                  ],
                )
              ],
            ),
          )
        ],
      ),
    );
  }
}
